/**
 * Copyright 2009 Joe LaPenna
 */

package com.googlecode.dumpcatcher.logging;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @author Joe LaPenna (joe@joelapenna.com)
 */
public class StackFormattingUtil {

    public static String getStackMessageString(Throwable e) {
        StringBuffer message = new StringBuffer();
        StackTraceElement[] stack = e.getStackTrace();
        StackTraceElement stackLine = stack[stack.length - 1];
        message.append(stackLine.getFileName());
        message.append(":");
        message.append(stackLine.getLineNumber());
        message.append(":");
        message.append(stackLine.getMethodName());
        message.append(" ");
        message.append(e.getMessage());
        return message.toString();
    }

    public static String getStackTraceString(Throwable tr) {
        if (tr == null) {
            return "";
        }
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        tr.printStackTrace(pw);
        return sw.toString();
    }

}
